<?php
namespace app\index\service;

use app\index\model\ArchiveTongModel;
use app\index\model\BidModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractKuanListModel;
use app\index\model\ContractShiModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\EnginZhengModel;
use app\index\model\IncomeEnginModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentEnginModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\model\WorkerModel;
use app\index\validate\CertBorrow;
use think\Db;

class WarnService
{
    public function contract_shi_enddt($data){
        //$工程名称$的$预警类型$于$合同竣工日期$到期，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = ContractShiModel::field('id,enginid,enddt,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('enddt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $enginInfo = EnginModel::get_info_by_pk($vvv['enginid']);
            $mess = $enginInfo['name'].'的'.$data['title'].'于'.$vvv['enddt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $vvv['id'],'contract_shi');
        }
        return true;
    }
    public function engin_bond_tuidt($data){
        //$工程名称$的$保证金类型$（$请款金额$）于$预计退回日期$到期，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginBondJiaoModel::field('id,enginid,bond_tuidt,bondtype,money,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('bond_tuidt','between',$checkQuen['date'])
            ->where('enginid','>',0)
            ->select()->toArray();

        foreach ($info as $vvv){
            $enginInfo = EnginModel::get_info_by_pk($vvv['enginid']);
            $bondtype = $this->get_engin_bondtype_name($vvv['bondtype']);
            $mess = $enginInfo['name'].'的'.$bondtype.'('.$vvv['money'].')于'.$vvv['bond_tuidt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $vvv['id'],'engin_bond_jiao');
        }
        return true;
    }
    public function contract_shi_zhidt($data){
        //$工程名称$的质保金（$质保金金额$）于$质保金到期日期$到期，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = ContractShiModel::field('id,enginid,zhidt,zhifee,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('zhidt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $enginInfo = EnginModel::get_info_by_pk($vvv['enginid']);
            $mess = $enginInfo['name'].'的质保金（'.$vvv['zhifee'].'）于'.$vvv['zhidt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $vvv['id'],'contract_shi');
        }
        return true;
    }
    public function contract_kuan_list_dt($data){
        //$工程名称$的第$明细序号$期回款（$回款金额$）于$计划回款时间$到期，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = ContractKuanListModel::alias('list')
            ->field('kuan.id,kuan.enginid,list.dt,list.money,kuan.created_user')
            ->join('contract_kuan kuan','kuan.id=list.kuanid')
            ->where(['kuan.comid'=>$data['comid'],'kuan.deleted_user'=>0])
            ->where('list.dt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $enginInfo = EnginModel::get_info_by_pk($vvv['enginid']);
            $mess = $enginInfo['name'].'的回款（'.$vvv['money'].'）于'.$vvv['dt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $vvv['id'],'contract_kuan');
        }
        return true;
    }
    public function engin_zheng_dt($data){
        //$工程名称$的整改通知（$整改通知编号$）逾期未完成整改或复检不通过，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $checkQuen['date'] = [
            date('Y-m-d', time()-$data['yutwo']*24*60*60),
            date('Y-m-d', time()-$data['yuone']*24*60*60),
        ];

        $info = EnginZhengModel::alias('zheng')
            ->field('zheng.id,zheng.enginid,xun.number as xunnumber,zheng.created_user')
            ->leftJoin('engin_xun xun','xun.id=zheng.xunid')
            ->where('zheng.dt','between',$checkQuen['date'])
            ->where('zheng.gaista', '=', 0)
            ->select()->toArray();

        foreach ($info as $vvv){
            $enginInfo = EnginModel::get_info_by_pk($vvv['enginid']);
            $mess = $enginInfo['name'].'的整改通知（'.$vvv['xunnumber'].'）逾期未完成整改或复检不通过，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $vvv['id'],'engin_zheng');
        }
        return true;
    }
    public function engin_hui_jin($data){
        //$工程名称$的产值进度已超过回款进度$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $shiinfo = ContractShiModel::where(['enginid'=>$vvv['enginid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])->find();
            if(!$shiinfo){
                continue;
            }
            $shiMoney = $shiinfo['money'];
            $shiMoney += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0,'status'=>1,'isturn'=>1])->sum('money');

            $leiChan = EnginChanModel::where(['deleted_user'=>0,'enginid'=>$vvv['enginid'],'status'=>1,'isturn'=>1])->sum('leijin');

            $gckid = $this->get_budshou_pid_by_name($vvv['comid'], '项目收入','工程款');
            $budshou = $this->get_budshou_sons($vvv['comid'], $gckid);
            $leiHuikuan = IncomeEnginModel::where(['deleted_user'=>0,'enginid'=>$vvv['enginid']])->where('budid','in',$budshou)->sum('money');


            $chanjin = 0;
            $huijin = 0;
            if($shiMoney != 0){
                $chanjin = $leiChan / $shiMoney;
                $huijin = $leiHuikuan / $shiMoney + $data['yuone'] / 100;
            }

            if( $chanjin <= $huijin ){
                continue;
            }

            $mess = $vvv['name'].'的产值进度已超过回款进度'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $shiinfo['id'],'contract_shi');
        }
        return true;
    }
    public function engin_cheng_zong($data){
        //$工程名称$的总成本已超过总预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->where('budid','in',[1,2,3,4,5])->sum('money');
            $chengfee += PaymentBaoModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->sum('shenfee');

            $rate = 0;
            if($budgetInfo['money'] != 0){
                $rate = $chengfee / $budgetInfo['money'];
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的总成本已超过总预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }
    public function engin_cheng_cai($data){
        //$工程名称$的材料成本已超过材料费预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid'],'budid'=>1])->sum('money');


            $topid = EnginBudsubModel::where(['comid'=>$vvv['comid'],'pid'=>0,'name'=>'材料费'])->value('id');
            $sonIds = EnginBudsubModel::where(['comid'=>$vvv['comid'],'topid'=>$topid])->column('id');
            $yusuan = EnginBudgetListModel::where(['budid'=>$budgetInfo['id']])
                ->where('subid','in',$sonIds)->sum('money');


            $rate = 0;
            if($yusuan != 0){
                $rate = $chengfee / $yusuan;
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的材料成本已超过材料费预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }
    public function engin_cheng_ren($data){
        //$工程名称$的人工成本已超过人工费预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid'],'budid'=>2])->sum('money');


            $topid = EnginBudsubModel::where(['comid'=>$vvv['comid'],'pid'=>0,'name'=>'人工费'])->value('id');
            $sonIds = EnginBudsubModel::where(['comid'=>$vvv['comid'],'topid'=>$topid])->column('id');
            $yusuan = EnginBudgetListModel::where(['budid'=>$budgetInfo['id']])
                ->where('subid','in',$sonIds)->sum('money');


            $rate = 0;
            if($yusuan != 0){
                $rate = $chengfee / $yusuan;
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的人工成本已超过人工费预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }
    public function engin_cheng_ji($data){
        //$工程名称$的机械设备成本已超过机械费预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid'],'budid'=>3])->sum('money');


            $topid = EnginBudsubModel::where(['comid'=>$vvv['comid'],'pid'=>0,'name'=>'机械费'])->value('id');
            $sonIds = EnginBudsubModel::where(['comid'=>$vvv['comid'],'topid'=>$topid])->column('id');
            $yusuan = EnginBudgetListModel::where(['budid'=>$budgetInfo['id']])
                ->where('subid','in',$sonIds)->sum('money');


            $rate = 0;
            if($yusuan != 0){
                $rate = $chengfee / $yusuan;
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的机械设备成本已超过机械费预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }
    public function engin_cheng_fen($data){
        //$工程名称$的分包成本已超过分包费预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentEnginModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid'],'budid'=>4])->sum('money');


            $topid = EnginBudsubModel::where(['comid'=>$vvv['comid'],'pid'=>0,'name'=>'分包费'])->value('id');
            $sonIds = EnginBudsubModel::where(['comid'=>$vvv['comid'],'topid'=>$topid])->column('id');
            $yusuan = EnginBudgetListModel::where(['budid'=>$budgetInfo['id']])
                ->where('subid','in',$sonIds)->sum('money');


            $rate = 0;
            if($yusuan != 0){
                $rate = $chengfee / $yusuan;
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的分包成本已超过分包费预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }
    public function engin_cheng_fei($data){
        //$工程名称$的项目费用成本已超过项目费用预算$预警规则$以上，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginModel::field('comid,id as enginid,name,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            $budgetInfo = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->find();
            if(!$budgetInfo){
                continue;
            }

            $chengfee = PaymentBaoModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$vvv['enginid']])->sum('shenfee');


            $topid = EnginBudsubModel::where(['comid'=>$vvv['comid'],'pid'=>0,'name'=>'项目费用'])->value('id');
            $sonIds = EnginBudsubModel::where(['comid'=>$vvv['comid'],'topid'=>$topid])->column('id');
            $yusuan = EnginBudgetListModel::where(['budid'=>$budgetInfo['id']])
                ->where('subid','in',$sonIds)->sum('money');


            $rate = 0;
            if($yusuan != 0){
                $rate = $chengfee / $yusuan;
            }

            if( $rate <= $data['yuone'] / 100 ){
                continue;
            }

            $mess = $vvv['name'].'的项目费用成本已超过项目费用预算'.$data['yuone'].'%以上，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 1, $mess, $budgetInfo['id'],'engin_budget');
        }
        return true;
    }



    public function bid_enddt_bao($data){
        //$工程名称$的$预警类型$于$报名截止时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = BidModel::field('id as bidid,enddt_bao,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('enddt_bao','between',$checkQuen['datetime'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $BidInfo = BidModel::get_info_by_pk($vvv['bidid']);
            $mess = $BidInfo['name'].'的'.$data['title'].'于'.$vvv['enddt_bao'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 2, $mess, $vvv['bidid'],'bid');
        }
        return true;
    }
    public function bid_enddt_tou($data){
        //$工程名称$的$预警类型$于$投标截止时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = BidModel::field('id as bidid,enddt_tou,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('enddt_tou','between',$checkQuen['datetime'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $BidInfo = BidModel::get_info_by_pk($vvv['bidid']);
            $mess = $BidInfo['name'].'的'.$data['title'].'于'.$vvv['enddt_tou'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 2, $mess, $vvv['bidid'],'bid');
        }
        return true;
    }
    public function bid_kaidt($data){
        //$工程名称$的$预警类型$于$开标时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = BidModel::field('id as bidid,kaidt,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('kaidt','between',$checkQuen['datetime'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $BidInfo = BidModel::get_info_by_pk($vvv['bidid']);
            $mess = $BidInfo['name'].'的'.$data['title'].'于'.$vvv['kaidt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 2, $mess, $vvv['bidid'],'bid');
        }
        return true;
    }
    public function bid_bond_tuidt($data){
        //$工程名称$的$保证金类型$（$请款金额$）于$预计退回日期$到期，请关注。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = EnginBondJiaoModel::field('id,bidid,bond_tuidt,bondtype,money,created_user')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('bond_tuidt','between',$checkQuen['date'])
            ->where('bidid','>',0)
            ->select()->toArray();

        foreach ($info as $vvv){
            $bidInfo = EnginModel::get_info_by_pk($vvv['bidid']);
            $bondtype = $this->get_engin_bondtype_name($vvv['bondtype']);
            $mess = $bidInfo['name'].'的'.$bondtype.'('.$vvv['money'].')于'.$vvv['bond_tuidt'].'到期，请关注。';
            $this->add_todo($this->get_duier_user($data, $vvv), 2, $mess, $vvv['id'],'engin_bond_jiao');
        }
        return true;
    }


    public function cert_expiry_dt($data){
        //$证书名称$（$持证人$）的$预警类型$于$有效期止|增项有效期$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertModel::alias('cert')
            ->field(Db::raw("cert.id,cert.id as certid,cert.created_user,cert.name,cert.personid,
            substring_index( substring_index( cert.expiry_dt, '[]', b.id), '[]', -1) AS expiry_dt"))
            ->leftJoin('help_topic b',Db::raw("b.id < ( length( cert.expiry_dt ) - length( REPLACE ( cert.expiry_dt, '[]', '' ) ) )/2+2"))
            ->where(['cert.comid'=>$data['comid'],'cert.deleted_user'=>0,'cert.state'=>1])
            ->select()->toArray();

        foreach ($info as $vvv){
            if($vvv['expiry_dt'] < $checkQuen['date'][0] || $vvv['expiry_dt'] > $checkQuen['date'][1]){
                continue;
            }
            $personName = CertPersonModel::where(['id'=>$vvv['personid']])->value('name');
            $mess = $vvv['name'].'（'.$personName.'）的'.$data['title'].'于'.$vvv['expiry_dt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert');
        }

        return true;
    }
    public function cert_electron_dt($data){
        //$证书名称$（$持证人$）的$预警类型$于$电子件有效期$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertModel::field('id,id as certid,created_user,electron_dt,name,personid')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('electron_dt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $personName = CertPersonModel::where(['id'=>$vvv['personid']])->value('name');
            $mess = $vvv['name'].'（'.$personName.'）的'.$data['title'].'于'.$vvv['electron_dt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert');
        }

        return true;
    }
    public function cert_inspect_dt($data){
        //$证书名称$（$持证人$）的$预警类型$于$下次年检时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertModel::field('id,id as certid,created_user,inspect_dt,name,personid')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('inspect_dt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $personName = CertPersonModel::where(['id'=>$vvv['personid']])->value('name');
            $mess = $vvv['name'].'（'.$personName.'）的'.$data['title'].'于'.$vvv['inspect_dt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert');
        }

        return true;
    }
    public function cert_company_expiry_dt($data){
        //$证书名称$（$所属单位$）的$预警类型$于$有效期止$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertCompanyModel::field('id,id as certcompanyid,created_user,expiry_dt,name,chanid')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('expiry_dt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $chanidName = DeptModel::get_name_by_pk($vvv['chanid']);
            $mess = $vvv['name'].'（'.$chanidName.'）的'.$data['title'].'于'.$vvv['expiry_dt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert_company');
        }

        return true;
    }
    public function cert_company_inspect_dt($data){
        //$证书名称$（$所属单位$）的$预警类型$于$下次年检时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertCompanyModel::field('id,id as certcompanyid,created_user,inspect_dt,name,chanid')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('inspect_dt','between',$checkQuen['date'])
            ->select()->toArray();

        foreach ($info as $vvv){
            $chanidName = DeptModel::get_name_by_pk($vvv['chanid']);
            $mess = $vvv['name'].'（'.$chanidName.'）的'.$data['title'].'于'.$vvv['inspect_dt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert_company');
        }

        return true;
    }
    public function cert_borrow_yudt($data){
        //$申请人$借用的$证书名称（明细第一条）$等的$预警类型$于$预计归还时间$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }



        $info = CertBorrowModel::field('id,created_user,yudt')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1])
            ->where('yudt','between',$checkQuen['date'])
            ->select()->toArray();


        foreach ($info as $vvv){
            $vvv['created_user_name'] = UserModel::get_realname_by_pk($vvv['created_user']);
            $oneInfo = CertBorrowListModel::where(['mid'=>$vvv['id']])->find();
            if($oneInfo['type'] == 1){
                $vvv['certid'] = $oneInfo['certid'];
                $certName = CertModel::where(['id'=>$oneInfo['certid']])->value('name');
            }else{
                $vvv['certcompanyid'] = $oneInfo['certid'];
                $certName = CertCompanyModel::where(['id'=>$oneInfo['certid']])->value('name');
            }




            $mess = $vvv['created_user_name'].'借用的'.$certName.'等的'.$data['title'].'于'.$vvv['yudt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 3, $mess, $vvv['id'],'cert_borrow');
        }

        return true;
    }


    public function cert_person_idcarddt($data){
        //$人员姓名$的身份证于$身份证有效期$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertPersonModel::field('id,name,created_user,userid,idcarddt')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('idcarddt','between',$checkQuen['date'])
            ->select()->toArray();


        foreach ($info as $vvv){
            $mess = $vvv['name'].'的身份证于'.$vvv['idcarddt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 4, $mess, $vvv['id'],'cert_person');
        }

        return true;
    }
    public function archive_tong_xianzhidt($data){
        //$人员$的劳动合同于$期限止$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = ArchiveTongModel::field('id,arcid,created_user,xianzhidt')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('xianzhidt','between',$checkQuen['date'])
            ->select()->toArray();


        foreach ($info as $vvv){
            $personInfo = CertPersonModel::where(['id'=>$vvv['arcid']])->find();
            $vvv['userid'] = $personInfo['userid'];

            $mess = $personInfo['name'].'的劳动合同于'.$vvv['xianzhidt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 4, $mess, $vvv['id'],'archive_tong');
        }

        return true;
    }

    public function cert_person_xianenddt($data){
        //$人员$的劳动合同于$期限止$到期，请及时处理。
        $checkQuen = $this->check_quen($data);
        if(!$checkQuen){
            return true;
        }

        $info = CertPersonModel::field('id,name,created_user,userid,xianenddt')
            ->where(['comid'=>$data['comid'],'deleted_user'=>0])
            ->where('xianenddt','between',$checkQuen['date'])
            ->select()->toArray();
        foreach ($info as $vvv){
            $mess = $vvv['name'].'的劳动合同于'.$vvv['xianenddt'].'到期，请及时处理。';
            $this->add_todo($this->get_duier_user($data, $vvv), 4, $mess, $vvv['id'],'cert_person');
        }

        return true;
    }

























    public function get_warn_duitemp(){
        return [
            'creater'  => '创建人',

            'engin_man_xiang'  => '项目负责人',
            'engin_man_zhuan'  => '项目专管员',
            'engin_fuzeer'  => '整改负责人',
            'engin_fujianer'  => '复检人',

            'bid_touer'  => '投标负责人',
            'bid_bianer'  => '标书制作人',

            'cert_jieer'  => '借证人',

            'cert_custodianid'  => '保管人',
            'cert_personid'  => '持证人',

            'person_userid'  => '本人（系统用户）',

            'pointer'  => '指定人员',
        ];
    }
    public function check_quen($data){
        if($data['havequen'] < 1){
            return true;
        }
        if($data['quen'] == 'w'){
            $w = date('w') == 0 ? 7 : date('w');
            if(!in_array($w, explode(',', $data['quenweek']))){
                return false;
            }
        }
        if($data['quen'] == 'm'){
            $d = date('d');
            if(!in_array($d, explode(',', $data['quendate']))){
                return false;
            }
        }

        $date = [
            date('Y-m-d', time()-$data['yuone']*24*60*60),
            date('Y-m-d', time()+$data['yutwo']*24*60*60),
        ];
        $datetime = [
            date('Y-m-d 00:00:00', time()-$data['yuone']*24*60*60),
            date('Y-m-d 23:59:59', time()+$data['yutwo']*24*60*60),
        ];

        $list['date'] = $date;
        $list['datetime'] = $datetime;
        return $list;
    }
    public function add_todo($uids, $nameType=1, $mess, $mid, $table, $meter=''){
        $name = '';
        if($nameType == 1){
            $name = '项目预警';
        }elseif ($nameType == 2){
            $name = '投标预警';
        }elseif ($nameType == 3){
            $name = '证书预警';
        }elseif ($nameType == 4){
            $name = '人员信息预警';
        }

        if(empty($uids)){
            return true;
        }
        foreach (array_unique(explode(',', $uids)) as $uid){
            $user = UserModel::get_info_by_pk($uid);
            if(!$user || $user['deleted_user'] > 0 || $user['status'] == 0){
                continue;
            }
            TodoModel::insert([
                'table' => $table,
                'meter' => $meter,
                'name' => $name,
                'mess' => $mess,
                'mid' => $mid,
                'uid' => $uid,
                'tododt' => date("Y-m-d H:i:s"),
            ]);
            WorkerModel::insert([
                'type' => 'todo',
                'uid' => $uid,
                'title' => '消息提醒',
                'mess' => $mess,
                'mid' => $mid,
                'table' => $table,
                'meter' => $meter,
                'dt' => date("Y-m-d H:i:s"),
            ]);

            if($user['status'] > 0 && !empty($user['wxuserid'])){
                $res = (new WxworkService())->send_text($user['wxuserid'], 'pagesShow/'.$table.'/show?id='.$mid, '消息提醒', $mess, date("Y-m-d H:i:s"));
            }
        }
        return true;
    }
    public function get_duier_user($data, $info){
        $uidArr = [];

        $superArr = UserModel::where(['id'=>$data['comid'],'deleted_user'=>0,'issuper'=>1])->column('id');
        foreach ($superArr as $supid){
            $uidArr[] = $supid;
        }


        $duierArr = explode(',', $data['duier']);
        foreach ($duierArr as $duiname){
            if($duiname == 'creater'){
                $uidArr[] = $info['created_user'];
            }
            if($duiname == 'engin_man_xiang'){
                $man_xiang = EnginModel::where(['id'=>$info['enginid']])->value('man_xiang');
                if($man_xiang){
                    $uidArr[] = $man_xiang;
                }
            }
            if($duiname == 'engin_man_zhuan'){
                $man_zhuan = EnginModel::where(['id'=>$info['enginid']])->value('man_zhuan');
                $uidArr = array_merge($uidArr, explode(',', $man_zhuan));
            }
            if($duiname == 'engin_fuzeer'){
                $fuzeer = EnginZhengModel::where(['id'=>$info['id']])->value('fuzeer');
                if($fuzeer){
                    $uidArr = array_merge($uidArr, explode(',', $fuzeer));
                }
            }
            if($duiname == 'engin_fujianer'){
                $fujianer = EnginZhengModel::where(['id'=>$info['id']])->value('fuzeer');
                if($fujianer){
                    $uidArr[] = $fujianer;
                }
            }

            if($duiname == 'bid_touer'){
                $touer = BidModel::where(['id'=>$info['bidid']])->value('touer');
                if($touer){
                    $uidArr[] = $touer;
                }
            }
            if($duiname == 'bid_bianer'){
                $bianer = BidModel::where(['id'=>$info['bidid']])->value('bianer');
                $uidArr = array_merge($uidArr, explode(',', $bianer));
            }

            if($duiname == 'cert_jieer'){
                $uidArr[] = $info['created_user'];
            }
            if($duiname == 'cert_custodianid'){
                if(isset($info['certid'])){
                    $custodianid = CertModel::where(['id'=>$info['certid']])->value('custodianid');
                }else{
                    $custodianid = CertModel::where(['id'=>$info['certcompanyid']])->value('custodianid');
                }
                if($custodianid){
                    $uidArr[] = $custodianid;
                }
            }
            if($duiname == 'cert_personid'){
                $personid = 0;
                if(isset($info['certid'])){
                    $personid = CertModel::where(['id'=>$info['certid']])->value('personid');
                }
                if($personid){
                    $uidArr[] = $personid;
                }
            }


            if($duiname == 'person_userid'){
                $uidArr[] = $info['userid'];
            }


            if($duiname == 'pointer'){
                $uidArr = array_merge($uidArr, explode(',', $data['zhier']));
            }
        }

        return implode(',', $uidArr);
    }

    public function get_engin_bondtype(){
        return [
            1 => '投标保证金',
            2 => '履约保证金',
            3 => '农民工工资保证金',
        ];
    }
    public function get_engin_bondtype_name($bondtype){
        $arr = $this->get_engin_bondtype();
        $str = isset($arr[$bondtype]) ? $arr[$bondtype] : '';
        return $str;
    }

    public function get_budshou_pid_by_name($comid, $topname, $name=''){
        $topid = EnginBudshouModel::where(['comid'=>$comid,'pid'=>0,'topid'=>0,'name'=>$topname])->value('id');
        $kemuid = EnginBudshouModel::where(['topid'=>$topid,'name'=>$name])->value('id');
        return $kemuid;
    }
    public function get_budshou_sons($comid, $id){
        $list = [$id];
        $seaarr = [$id];

        while (count($seaarr) > 0) {
            $son = EnginBudshouModel::where(['comid'=>$comid,'status'=>1,'deleted_user'=>0])
                ->where('pid','in',$seaarr)->column('id');

            $seaarr = [];
            foreach ($son as $v){
                $list[] = $v;
                $seaarr[] = $v;
            }
        }
        return $list;
    }






}
